<?php 
include('conexion.php');
$idFact=$_POST['idFact'];
$usuario->consulta('SELECT * FROM factura WHERE numero_factura='.$idFact.'');
$a = $usuario->extraer_registro();
$status = $a['tipo'];
$iva= $a['iva'];
$usuario2 = new Servidor_Base_Datos($servidor,"root",$pass,$base_datos);
$usuario3 = new Servidor_Base_Datos($servidor,"root",$pass,$base_datos);
$i=0;
$usuario->consulta('SELECT * FROM factura_detalle WHERE fk_factura='.$idFact.'');
if ($a=$usuario->extraer_registro()){
	if ($status==1){
		echo'<h4>&nbsp;&nbsp;&nbsp;&nbsp;<strong>Factura </strong><span class="blue">Abierta</span></h4>';
		}
	else if($status==2){
		echo'<h4>&nbsp;&nbsp;&nbsp;&nbsp;<strong>Factura </strong><span class="blue">Por Cobrar</span></h4>';
		}
	else if($status==3){
		echo'<h4>&nbsp;&nbsp;&nbsp;&nbsp;<strong>Factura </strong><span class="blue">Cobrada</span></h4>';
		}
			$usuario->consulta("SELECT SUM(monto) from forma_pago where fk_factura='$idFact'");
			if	($montoPagado1=$usuario->extraer_registro()){
				$montoPagado1=$montoPagado1['SUM(monto)'];
				$usuario->consulta("SELECT * from factura where numero_factura='$idFact'");
				$factura1=$usuario->extraer_registro();
				$total1=($factura1['total']*($factura1['iva']/100))+$factura1['total'];
				$montoPorPagar1= $total1-$montoPagado1;
				
				echo'<h4>&nbsp;&nbsp;&nbsp;&nbsp;<strong>Monto por Cobrar: </strong>'.round($montoPorPagar1,2).'</h4>';
			}
			
			$usuario2->consulta("SELECT c.nombre FROM cliente c, factura f WHERE f.numero_factura= ".$idFact."
																	 and f.fk_cliente = c.numero_cliente");
			$cli = $usuario2->extraer_registro();
			$usuario3->consulta("SELECT e.nombre, e.apellido FROM empleado e, factura f WHERE f.numero_factura=".$idFact."
																	 and f.fk_empleado = e.numero_empleado");
			$emp = $usuario3->extraer_registro();
			echo'<h4><strong>&nbsp;&nbsp;&nbsp;&nbsp;Codigo de Factura: </strong>'.$idFact.' | <strong>Cliente: </strong>'.$cli['nombre'].' <br></br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;Empleado: </strong>'.$emp['nombre'].' '.$emp['apellido'].'</h4>';
			$usuario->consulta('SELECT * FROM factura_detalle WHERE fk_factura='.$idFact.'');
			echo'&nbsp;&nbsp;&nbsp;&nbsp;<div align="center"><table align="center" width="50%">
			<tr>
				<td align="Center">Codigo</td>
				<td align="Center">Producto</td>
				<td align="Center">Cantidad</td>
				<td align="Center">Precio</td>	
			</tr>';
			while($deta = $usuario->extraer_registro()){
				$i++;//#999999
				if ($i%2==1){
					echo'<tr bgcolor="#CCCCCC" align="center">';	
					}
				else{
					echo'<tr bgcolor="#CCCCCC" align="center">';
					}
					$numP=$deta['producto_numero_producto'];
					$usuario2->consulta("SELECT * FROM producto WHERE numero_producto='$numP'");
					$detalle = $usuario2->extraer_registro();
					echo'<td>'.$detalle['codigo'].'</td>
						 <td>'.$detalle['nombre'].'</td>
						 <td>'.$deta['cantidad'].'</td>
						 <td>'.$detalle['precio'].'</td></tr>';
						 $total = $detalle['precio']*$deta['cantidad']+$total;
				}
	echo'</table >';
						//$usuario->consulta('SELECT iva from empresa');
						//$iva = $usuario->extraer_registro();
						$iva = $iva/100;
						$ivaTotal = $total*$iva;
						$total = $total+$ivaTotal;
						$iva=$iva*100;
	//$iva = $total*0.12;
    //$total = $total+$iva;
	echo'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I.V.A('.$iva.'%): '.$ivaTotal.'<br></br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Total: '.$total.'<br></br>';
	if ($status==1){
		echo		'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><a href="#" style="color:#006" align="right" onclick="retomarFactura('.$idFact.')">Retomar</a> | <a href="#" style="color:#006" align="right" onclick="eliminarFactura('.$idFact.')">Eliminar</a> | <a href="#" style="color:#006" align="right" onclick="ocultarInfo('.$idFact.')">Ocultar</a></strong></div>';
	}
	else if ($status==2){
	//cobrar
	echo		'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="generarFacturaPdf.php?factura='.$idFact.'" target="_blank" title="PDF Factura"><img src="images/pdfChiqui.png" /></a><strong><a href="#" style="color:#006" align="right" onclick="cobrarFactura('.$idFact.')">Cobrar</a> | <a href="#" style="color:#006" align="right" onclick="ocultarInfo('.$idFact.')">Ocultar</a></strong></div>';
	
	}
	else if ($status==3){
	//cobrada
	echo		'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="generarFacturaPdf.php?factura='.$idFact.'" target="_blank" title="PDF Factura"><img src="images/pdfChiqui.png" /></a><strong><a href="#" style="color:#006" align="right" onclick="ocultarInfo('.$idFact.')">Ocultar</a></strong></div>';
	
	}
}
	else{
		echo'<h4><strong>Disculpe la venta no tiene ningun producto</h4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><a href="#" style="color:#006" align="right" onclick="retomarFactura('.$idFact.')">Retomar</a> | <a href="#" style="color:#006" align="right" onclick="eliminarFactura('.$idFact.')">Eliminar</a> | <a href="#" style="color:#006" align="right" onclick="ocultarInfo('.$idFact.')">Ocultar</a></strong></div>';
		}
?>